package com.ian.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * @author Witt
 * @version 1.0.0
 * @date 2022/5/14
 */
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    /*@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests(requests -> requests.anyRequest().authenticated())
                .formLogin(formLogin -> formLogin.permitAll())
//                .csrf().disable()
        ;
    }*/

    /**
     * In memory
     * @return UserDetailsService类型的对象
     */
    @Override
    @Bean
//    @Bean("myUserDetailsService")
    public UserDetailsService userDetailsService() {
        return new InMemoryUserDetailsManager(
                User.builder()
                        .username("user")
                        // 密码明文是 123
                        .password("{bcrypt}$2a$10$EOWPKZDdcX5u1SYprJVDy.7puK7D/NVEEBdhutPCpJeicssrlLhJm")
                        // 密码明文是 password
//                        .password("{bcrypt}$2a$10$lmURdwCsjA9jFc5NgzX1Keq5O91WEuOOgEtMQ246X.G/cu.HSqMTG")
                        .roles("user")
                        .build(),
                User.withUsername("admin")
                        // 密码明文是 123
                        .password("{bcrypt}$2a$10$EOWPKZDdcX5u1SYprJVDy.7puK7D/NVEEBdhutPCpJeicssrlLhJm")
                        .roles("admin")
                        .build());
    }

}
